package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {

    @Reference
    RoleService roleService;

    //新增
    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds,Integer[] menuIds){
        log.debug("RoleController:add : role:" +role);
        log.debug("RoleController:add : permissionIds: " +permissionIds);
        roleService.add(role,permissionIds,menuIds);
        log.debug("添加角色成功");
        //新增成功
        return new Result(true, MessageConst.ADD_CHECKGROUP_SUCCESS);
    }

    //分页查询
    @RequestMapping("/findPage")
    public Result findByPage(@RequestBody QueryPageBean queryPageBean){
        log.debug("RoleController{"+queryPageBean+"}");
        PageResult pageResult = roleService.findByPage(queryPageBean);
        log.debug("角色分页查询成功!!");
        return new Result(true, MessageConst.QUERY_CHECKGROUP_SUCCESS, pageResult);
    }

    //角色删除
    @RequestMapping("/delById")
    public Result delById(Integer id){
        log.debug("RoleController:delete: " + id);
        roleService.delById(id);
        log.debug("角色组删除成功!!");
        return new Result(true, MessageConst.DELETE_CHECKGROUP_SUCCESS);
    }

    //回显角色查询
    @RequestMapping("/findById")
    public Result findById(Integer id){
        log.debug("RoleController:findById: " + id);
       Role role = roleService.findById(id);
        log.debug("角色查询成功!!");
        return new Result(true, MessageConst.QUERY_CHECKGROUP_SUCCESS,role);
    }


    //回显根据角色查询查询权限
    @RequestMapping("/findpermissionIdsById")
    public Result findpermissionIdsById(Integer id){
        log.debug("RoleController:findpermissionIdsById: " + id);
        List<Integer> permissionIds = roleService.findpermissionIdsById(id);
        log.debug("根据角色id查询权限项id成功!!");
        return new Result(true, MessageConst.QUERY_CHECKGROUP_SUCCESS,permissionIds);
    }

    //回显根据角色查询查询权限
    @RequestMapping("/findmwnuIdsById")
    public Result findmwnuIdsById(Integer id){
        log.debug("RoleController:findmwnuIdsById: " + id);
        List<Integer> findmwnuIdsById = roleService.findmenuIdsById(id);
        log.debug("根据角色id查询菜单项id成功!!");
        return new Result(true, MessageConst.QUERY_CHECKGROUP_SUCCESS,findmwnuIdsById);
    }

    //修改角色
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role, Integer[] permissionIds,Integer[] menuIds){
        roleService.edit(role,permissionIds,menuIds);
        log.debug("角色组修改成功！！！");
        return new Result(true,MessageConst.EDIT_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/findAll")
    public Result findAll() {
        log.debug("查询所有角色,打通前端页面成功");
        List<Role> checkItemList = roleService.findAll();
        log.debug("查询多所有角色所有成功!!");
        return new Result(true, "查询多所有角色所有成功!!", checkItemList);
    }
}
